#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.db import models
import datetime

class Question(models.Model):
    """Definition of Question in QA System

    Necessary Fields are:
        title --> limit 100 characters
        description --> no limit
        questioner --> ForeignKey
        question_datetime --> when create a question, assign it with datetime.datetime.now
        view_count --> when create a question, assign it with 0
	pro_record -->keep a record of people who like the question
        against_record --> keep a record of people who are against the question
	favourite_record -->keep a record of people who place it as their favourite question

    Optional Fields are:
        tags --> a question can have no tag
        followers --> a question can have no followers
        groups --> a question can @ 0-n groups
        
    """

    title = models.CharField(max_length = 100)
    description = models.TextField()
    questioner = models.ForeignKey('SNS.User', related_name = 'questions')
    question_datetime = models.DateTimeField(default = datetime.datetime.now)
    view_count = models.IntegerField(default = 0)
    vote_count = models.IntegerField(default = 0)
    answered_count = models.IntegerField(default = 0)
    tags = models.ManyToManyField('QATag', null = True, blank = True)
    pro_record = models.ManyToManyField('SNS.User',null = True,blank = True,related_name = 'pro_question')
    against_record = models.ManyToManyField('SNS.User',null = True,blank = True,related_name = 'against_question')
    favourite_record = models.ManyToManyField('SNS.User',null = True,blank = True,related_name = 'favourite_question')
    followers = models.ManyToManyField('SNS.User', related_name = 'follow_questions', null = True, blank = True)
    groups = models.ManyToManyField('Group.Group', related_name = 'questions',
                                    null = True, blank = True)

    def __unicode__(self):
        return self.title
    

class Answer(models.Model):
    """Definition of Answer in QA System

    Necessary Fields are:
        question --> ForeignKey
        description --> no limit
        answerer --> ForeignKey
        answer_datetime --> when append an answer, assign it with datetime.datetime.now
        vote_count --> when create an answer, assign it with 0
	pro_record -->keep a record of people who like the answer
        against_record --> keep a record of people who are against the answer
    """

    class Meta:
        unique_together = ('question', 'answerer')

    question = models.ForeignKey('Question')
    description = models.TextField()
    answerer = models.ForeignKey('SNS.User')
    answer_datetime = models.DateTimeField(default = datetime.datetime.now)
    vote_count = models.IntegerField(default = 0)
    pro_record = models.ManyToManyField('SNS.User',null = True,blank = True,related_name = 'pro_answer')
    against_record = models.ManyToManyField('SNS.User',null = True,blank = True,related_name = 'against_anwer')


    def __unicode__(self):
        return 'Re: ' + self.question.title

class QATag(models.Model):
    """Definition of QATag in QA System

    Necessary Fields are:
        name  --> tag name for topics, unique
    
    """

    name = models.CharField(unique = True, max_length = 30)
    user = models.ManyToManyField('SNS.User', null = True, blank = True)
    count = models.IntegerField(default = 1)
    def __unicode__(self):
        return self.name
  
